package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.NewscategoryDaolmpl;
import com.ysd.dao.impl.ProductcategoryDaolmpl;
import com.ysd.entity.Newscategory;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminNewscategoryServlet extends BaseServlet {
	//��ȡ��Ѷ����
	//·����localhost:8080/dayixiacedexiangmu/adminNewscategory?reqName=getNewscategory
	public void getNewscategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		NewscategoryDaolmpl ncDao = new NewscategoryDaolmpl();
		List<Newscategory> ncList =ncDao.getNewscategoryList();
		
		request.setCharacterEncoding("UTF-8");//������Ӧ�ı����ʽ
		PrintWriter out = response.getWriter();//out����
		
		Gson gson = new Gson();//����Gson����
		
		String res = gson.toJson(ncList);//��ncListת��Ϊjson��ʽ�ַ���
		out.print(res);//��Ӧ
		
		out.flush();
		out.close();
	}
	/**
	 * localhost:8080/dayixiacedexiangmu/adminNewscategory?reqName=delNewscategory
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delNewscategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//����ǰ̨�����Ĳ���
		int ncId = Integer.parseInt(request.getParameter("ncId"));
		NewscategoryDaolmpl ncDao = new NewscategoryDaolmpl();
		request.setCharacterEncoding("UTF-8");//������Ӧ�ı����ʽ
		PrintWriter out = response.getWriter();//out����
		int n  = ncDao.delNewscategoryId(ncId);//����ʵ�����ɾ��������������Ӱ��
		if (n>0) {//ɾ���ɹ�
			out.print(Result.toClient(true, "ɾ���ɹ�",""));
		}else {//ɾ������
			out.print(Result.toClient(false, "ɾ��ʧ��",""));
		}
		
		
		out.flush();
		out.close();
	}
	/**
	 * localhost:8080/manmanzahuopu/adminNewscategory?reqName=addNewscategory
	 * 添加
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addNewscategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	   response .setCharacterEncoding("UTF-8");
	   PrintWriter out = response.getWriter();
		
	   NewscategoryDaolmpl ncDao = new NewscategoryDaolmpl();// 实现类对象
	   
		String newsCategoryName = request.getParameter("newsCategoryName");//接收前台传来的参数
		if(newsCategoryName!=null&&!newsCategoryName.equals("")) {
			List<Newscategory>ncList = ncDao.getnewscategoryByName(newsCategoryName);
			if (ncList.size()==0){// 没有查到可以添加

				Newscategory nc = new Newscategory(0, newsCategoryName);// 创建产品分类对象
				
				
			    int n = ncDao.addNewcategory(nc);
			    if (n>0) {
					out.print(Result.toClient(true, "添加成功",""));
				} else {
					out.print(Result.toClient(false, "添加失败",""));
				}

			} else {
				out.print(Result.toClient(false, "已存在",""));
			}
		}
			 
		out.flush();
		out.close();
	}
	/**
	 * localhost:8080/dayixiacedexiangmu/adminNewscategory?reqName=getNewscategoryById
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getNewscategoryById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	   response .setCharacterEncoding("UTF-8");
	   PrintWriter out = response.getWriter();
		
	   NewscategoryDaolmpl ncDao = new NewscategoryDaolmpl();// 实现类对象
	   int newId = Integer.parseInt(request.getParameter("nId"));// 接收前台传来的参数 转化为int类型
	   Newscategory nc=ncDao.getNewscategoryById(newId);
	   
	   Gson gson = new Gson();
	   String res = gson.toJson(nc);// 将nc对象转化为json字符串
	   out.print(res);// 响应数据
			 
		out.flush();
		out.close();
	}
	/**
	 * localhost:8080/dayixiacedexiangmu/adminNewscategory?reqName=updateNewscategoryById
	 * 修改
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateNewscategoryById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	   response .setCharacterEncoding("UTF-8");
	   PrintWriter out = response.getWriter();
		
	   NewscategoryDaolmpl ncDao = new NewscategoryDaolmpl();// 实现类对象
	   int newsCategoryid = Integer.parseInt(request.getParameter("newcateId"));
	   String newsCategoryName = request.getParameter("Newsname");
	   Newscategory nc=new Newscategory(newsCategoryid, newsCategoryName);
	   ncDao.updateNewscategoryById(nc);
	    int b = ncDao.updateNewscategoryById(nc);
	    if(b>0){
	    	out.print(Result.toClient(true, "修改成功",""));
	    }else {
	    	out.print(Result.toClient(false, "修改失败",""));

		}
		out.flush();
		out.close();
	}
}
